package com.internship.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.internship.model.City;
import com.internship.model.DepartmentInfo;
import com.internship.model.EnterpriseInfo;
import com.internship.model.JobInfo;
import com.internship.model.Jobenterprise;
import com.internship.model.Province;
import com.internship.page.Page;
import com.internship.service.Const;
import com.internship.service.DepartmentInfoService;
import com.internship.service.EnterpriseInfoService;
import com.internship.service.JobInfoService;


@Controller
@RequestMapping("jobinfo")
public class JobInfoController {
	private final Logger logger = Logger.getLogger(JobInfoController.class);
	
	@Autowired
	private DepartmentInfoService 	departmentInfoService;
	@Autowired
	private EnterpriseInfoService 	enterpriseinfoService;
	@Autowired
	private JobInfoService 	jobinfoService;
	private List<DepartmentInfo>  departmentInfoList = null;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}

	@RequestMapping("/jobinfo_list.do")
	public ModelAndView getJobinfoList() {
		return getJobinfoList(null,null,1);
	}
	
	@RequestMapping("/jobinfo_page_list.do")
	public ModelAndView getJobinfoList(@RequestParam String XYDM,@RequestParam String JobName,@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("job_info");
		HttpSession session = getSession();
		try {
			if (String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("2")||String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("1")) {
				Page<JobInfo> pageList = jobinfoService.getJobinfoList(XYDM, JobName, pageNumber);
				this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
				mv.addObject("currentPage", pageNumber);
				mv.addObject("XYDM", XYDM);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("departmentInfoList",departmentInfoList);
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("jobInfoList", pageList.getResultList());
			}else {
				Page<JobInfo> pageList = jobinfoService.getJobinfoList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)),JobName, pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("jobInfoList", pageList.getResultList());
			}
		mv.addObject("JobName", JobName); 
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return mv;
	}
	/**?
	 * 添加实习岗位
	 * @return
	 */
	@RequestMapping("/add_jobInfo.do")
	public ModelAndView addJobInfo() {
		ModelAndView mv = new ModelAndView("job_info_add");
		this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
		mv.addObject("departmentInfoList",departmentInfoList);
		return mv;
	}
	@RequestMapping("/jobInfo_save.do")
	@ResponseBody
	public JSONObject saveJobInfo(@RequestParam("XYDM")String  XYDM,@RequestParam("JobName") String JobName,@RequestParam("JobIntroduce") String JobIntroduce,@RequestParam("TechnicalAbilityNeed") String TechnicalAbilityNeed ) {
		JSONObject json = new JSONObject();
		HttpSession session = getSession();
		try {
			JobInfo jobInfo = new JobInfo();
			if(String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("2")||String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("1")) {
				jobInfo.setXYDM(XYDM);
			}else {
				jobInfo.setXYDM(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)));
			}
			jobInfo.setJobName(JobName);
			jobInfo.setJobIntroduce(JobIntroduce);
			jobInfo.setTechnicalAbilityNeed(TechnicalAbilityNeed);
			int rel = jobinfoService.addJobinfo(jobInfo);
			json.put("result", rel>0?"success":"error");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return json;
	}
	/**?
	 * 修改实习岗位
	 * @return
	 */
	@RequestMapping("/edit_jobInfo.do")
	public ModelAndView editJobInfo(@RequestParam("Id")int  Id) {
		ModelAndView mv = new ModelAndView("job_info_edit");
		this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
		mv.addObject("departmentInfoList",departmentInfoList);
		JobInfo jobInfo = jobinfoService.getJobinfo(Id);
		mv.addObject("jobInfo",jobInfo);
		return mv;
	}
	@RequestMapping("/jobInfo_update.do")
	@ResponseBody
	public JSONObject updateJobInfo(@RequestParam("Id")int  Id,@RequestParam("JobName") String JobName,@RequestParam("JobIntroduce") String JobIntroduce,@RequestParam("TechnicalAbilityNeed") String TechnicalAbilityNeed ) {
		JSONObject json = new JSONObject();
		try {
			JobInfo jobInfo = jobinfoService.getJobinfo(Id);
			jobInfo.setJobName(JobName);
			jobInfo.setJobIntroduce(JobIntroduce);
			jobInfo.setTechnicalAbilityNeed(TechnicalAbilityNeed);
			int rel = jobinfoService.updateJobinfo(jobInfo);
			json.put("result", rel>0?"success":"error");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return json;
	}
	/**?
	 * 删除岗位
	 * @param Id
	 * @return
	 */
		@RequestMapping("/delete.do")
		@ResponseBody
		public JSONObject delJobinfo(@RequestParam int Id){
			JSONObject json = new JSONObject();
			int  rel= jobinfoService.delJobinfo(Id);
			json.put("result", rel > 0 ?"success":"error");
			 return json; 
		}
		/**?
		 * 查看单位
		 * @param Id
		 * @return
		 */
		
		@RequestMapping("/job_enter_list.do")
		public ModelAndView getJobEnterList(@RequestParam int Id) {
			ModelAndView mv = new ModelAndView("job_enter_list");
			HttpSession session = getSession();
			try {
					List<EnterpriseInfo> pageList = jobinfoService.getEnterpriseEnterList(Id);
					JobInfo jobInfo = jobinfoService.getJobinfo(Id);
					
					mv.addObject("name",jobInfo.getJobName());
					mv.addObject("id",jobInfo.getId());
					mv.addObject("EnterpriseInfoList",pageList);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
			return mv;
		}
		/**?
		 * 删除岗位所关联的单位
		 * @param Id
		 * @return
		 */
			@RequestMapping("/delJobenterprise.do")
			@ResponseBody
			public JSONObject delJobenterprise(@RequestParam int EnterpriseId,@RequestParam int JobId){
				JSONObject json = new JSONObject();
				int  rel= jobinfoService.delJobenterprise(EnterpriseId, JobId);
				json.put("result", rel > 0 ?"success":"error");
				 return json; 
			}
		/**?
		 * 添加单位
		 * @return
		 */
		private List<Province>  provinceList = null;
		private List<City>  cityList = null;
		@RequestMapping("/add_enterpriseinfo.do")
		public ModelAndView addEnterpriseinfo(@RequestParam int id) {
				ModelAndView mv = new ModelAndView("job_enter_list_add");
				this.provinceList = enterpriseinfoService.getProvince();
				this.cityList = enterpriseinfoService.getCity(provinceList.get(0).getProvinceId());
				List<EnterpriseInfo> enterpriseinfoList = enterpriseinfoService.getEnterpriseinfoEnterList(provinceList.get(0).getProvinceId(), cityList.get(0).getCityId());
				mv.addObject("provinceList",provinceList);
				mv.addObject("cityList",cityList);
				mv.addObject("enterpriseinfoList",enterpriseinfoList);
				mv.addObject("id",id);
		return mv;
	}
		@RequestMapping("/province.do")
		@ResponseBody
		public JSONObject province(@RequestParam("ProvinceId")int ProvinceId  ) {
			JSONObject json = new JSONObject();
			this.cityList = enterpriseinfoService.getCity(ProvinceId);
			json.put("cityList", cityList);
			List<EnterpriseInfo> enterpriseinfoList = enterpriseinfoService.getEnterpriseinfoEnterList(ProvinceId,cityList.get(0).getCityId());
			json.put("enterpriseinfoList", enterpriseinfoList);
			return json;
		}
		@RequestMapping("/city.do")
		@ResponseBody
		public JSONObject city(@RequestParam("cityId")int cityId  ) {
			JSONObject json = new JSONObject();
			List<EnterpriseInfo> enterpriseinfoList = enterpriseinfoService.getEnterpriseinfoEnterList(0,cityId);
			json.put("enterpriseinfoList", enterpriseinfoList);
			return json;
		}
		/**?
		 * 新增企业
		 */
		@RequestMapping("/enterpriseinfo_save.do")
		@ResponseBody
		public JSONObject saveEnterpriseinfo(@RequestParam("JobId")int  JobId,@RequestParam("Name")String  Name,@RequestParam("provinceId") int provinceId,@RequestParam("cityId") int cityId,@RequestParam("TheAddress") String TheAddress,@RequestParam("TheContact") String TheContact,@RequestParam("TheContactPhone") String TheContactPhone,@RequestParam("TheIntroduce") String TheIntroduce,@RequestParam("EnterpriseSpecialReq") String EnterpriseSpecialReq ) {
			JSONObject json = new JSONObject();
			EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
			enterpriseInfo.setName(Name);
			enterpriseInfo.setTheCity(cityId);
			enterpriseInfo.setTheProvince(provinceId);
			enterpriseInfo.setTheAddress(TheAddress);
			enterpriseInfo.setTheContact(TheContact);
			enterpriseInfo.setTheContactPhone(TheContactPhone);
			enterpriseInfo.setTheIntroduce(TheIntroduce);
			enterpriseInfo.setCreateType(0);
			int rel = enterpriseinfoService.addEnterpriseinfo(enterpriseInfo);
			if (rel>0){
				Jobenterprise  jobenterprise = new Jobenterprise();
				jobenterprise.setEnterpriseId(enterpriseInfo.getId());
				jobenterprise.setJobId(JobId);
				jobenterprise.setEnterpriseSpecialReq(EnterpriseSpecialReq);
				jobinfoService.addJobenterprise(jobenterprise);
			}
			json.put("result", rel>0?"success":"error");
			return json;
		}
		/**
		 * 已有企业
		 * @param JobId
		 * @param EnterpriseId
		 * @param EnterpriseSpecialReq
		 * @return
		 */
		@RequestMapping("/jobenterprise_save.do")
		@ResponseBody
		public JSONObject saveEnterpriseinfo(@RequestParam("JobId")int  JobId,@RequestParam("EnterpriseId") int EnterpriseId,@RequestParam("EnterpriseSpecialReq") String EnterpriseSpecialReq ) {
			JSONObject json = new JSONObject();
				Jobenterprise  jobenterprise = new Jobenterprise();
				jobenterprise.setEnterpriseId(EnterpriseId);
				jobenterprise.setJobId(JobId);
				jobenterprise.setEnterpriseSpecialReq(EnterpriseSpecialReq);
			int rel = 	jobinfoService.addJobenterprise(jobenterprise);
			json.put("result", rel>0?"success":"error");
			return json;
		}
}